package dq.study.springcache;

import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author dongqun
 * @date 2020/8/24 15:23
 */
@Service
@CacheConfig(cacheNames = "test_service",cacheManager = "myRedisManager")
public class TestService {

    @Cacheable(cacheNames = "add")
    public int add() {
        System.out.println("计算1+1...");
        return 1 + 1;
    }

    @Cacheable
    public int add(int a, int b) {
        System.out.println("计算a+b...");
        return a + b;
    }

    @Cacheable
    public int reduce(int a, int b) {
        System.out.println("计算a-b...");
        return a - b;
    }


    @CacheEvict("add")
    public void evit() {
        System.out.println("1+1缓存失效");
    }


    @Cacheable(cacheNames = "getOne",key = "#id")
    public Test getOne(int id) {
        System.out.println("查询test" + id);
        Test test = new Test();
        test.setId(id);
        test.setContent("sss");
        test.setData(new Date());
        return test;
    }

    @CachePut(cacheNames = "getOne",key = "#result.id")
    public Test udpate(int id, String content) {
        Test test = new Test();
        test.setId(id);
        test.setContent(content);
        test.setData(new Date());
        return test;
    }

    @CacheEvict(allEntries = true)
    public void evitAll() {
        System.out.println("所有缓存失效");
    }

    @Cacheable(keyGenerator = "myKeyGenerator",cacheManager = "myRedisManager")
    public User getUser(String name,Integer age,String sex){
        User user = new User();
        user.setName(name);
        user.setAddress("青岛");
        user.setAge(age);
        user.setSex(sex);
        return user;
    }
}
